﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Threading;

namespace MuseumQuiz
{
    public partial class MainPage : UserControl
    {
        private int score = 0;

        public int Score
        {
            get { return score; }
            set { score = value; }
        }

        private bool correct = false;

        public bool Correct
        {
            get { return correct; }
            set { correct = value; }
        }

        private List<MuseumService.QuizVragen> dedrievragen = new List<MuseumService.QuizVragen>();

        public List<MuseumService.QuizVragen> Dedrievragen
        {
            get { return dedrievragen; }
            set { dedrievragen = value; }
        }

        private List<MuseumService.QuizVragen> reedsgesteldevragen = new List<MuseumService.QuizVragen>();

        public List<MuseumService.QuizVragen> Reedsgesteldevragen
        {
            get { return reedsgesteldevragen; }
            set { reedsgesteldevragen = value; }
        }

        public MainPage()
        {
            InitializeComponent();

            GetTheQuestions(Score);
        }

        void GetTheQuestions(int score)
        {
            //MuseumService
            MuseumService.MuseumQuizServiceClient mijnproxy = new MuseumService.MuseumQuizServiceClient();
            mijnproxy.Get3RandomQuestionsCompleted += new EventHandler<MuseumService.Get3RandomQuestionsCompletedEventArgs>(mijnproxy_Get3RandomQuestionsCompleted);
            mijnproxy.Get3RandomQuestionsAsync();
        }

        void mijnproxy_Get3RandomQuestionsCompleted(object sender, MuseumService.Get3RandomQuestionsCompletedEventArgs e)
        {
            Dedrievragen = (List<MuseumService.QuizVragen>)e.Result.ToList();

            foreach (MuseumService.QuizVragen q in Dedrievragen)
            {
                if (Reedsgesteldevragen.Count != 0)
                {
                    if (Reedsgesteldevragen.Contains(q))
                    {
                        GetTheQuestions(Score);
                    }
                }
            }

            image1.Source = new BitmapImage(new Uri("/MuseumQuiz;component/Images/" + Dedrievragen[0].vraagFotoLink, UriKind.Relative));
            image2.Source = new BitmapImage(new Uri("/MuseumQuiz;component/Images/" + Dedrievragen[1].vraagFotoLink, UriKind.Relative));
            image3.Source = new BitmapImage(new Uri("/MuseumQuiz;component/Images/" + Dedrievragen[2].vraagFotoLink, UriKind.Relative));

            Random r = new Random();
            int randomint = r.Next(0, e.Result.Count);

            textBlockVraag.Text = Dedrievragen[randomint].vraagTXT;
            textBlockScore.Text = Score.ToString();
        }

        void mijnproxy_DoWorkCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            throw new NotImplementedException();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void image2_ImageFailed(object sender, ExceptionRoutedEventArgs e)
        {

        }

        private void image3_ImageFailed(object sender, ExceptionRoutedEventArgs e)
        {

        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {

        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {

        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
        }

        private void WrongAnswer()
        {
            Correct = false;
            image1.Source = null;
            image2.Source = null;
            image3.Source = null;
            textBlockVraag.Text = "U antwoordde fout. GAME OVER;";
            Continuu.Begin();
            //Thread.Sleep(200);
            HighScoreWindow hsw = new HighScoreWindow();
            hsw.Show();

        }
        private void Restart()
        {

            score = 0;
            //
            GetTheQuestions(Score);
            // RestartKnop.Begin();
            //Thread.Sleep(1000);
            StartKnop.Begin();
            // EmptyTheField.Begin();


        }
        private void button4_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            Restart();

            // TODO: Add event handler implementation here.


        }
        private void button6_Click(object sender, System.Windows.RoutedEventArgs e)
        {

            EmptyTheField.Begin();
            // TODO: Add event handler implementation here.
        }
        private void button1_MouseEnter(object sender, MouseEventArgs e)
        {

        }

        private void image1_ImageFailed(object sender, ExceptionRoutedEventArgs e)
        {

        }

        private void click1(object sender, RoutedEventArgs e)
        {
            //    if (Dedrievragen[0].vraagTXT == textBlockVraag.Text)
            //    {
            //        Correct = true;
            //        Score++;
            //        textBlockVraag.Text = "Correct !!!";
            //        GetTheQuestions(Score);
            //        Reedsgesteldevragen.Add(Dedrievragen[0]);
            //    }
            //    else WrongAnswer();
        }

        private void button1_MouseEnter_1(object sender, MouseEventArgs e)
        {
            Button1Animation.Begin();
        }

        private void button2_Click_1(object sender, RoutedEventArgs e)
        {

        }

        private void button3_Click_1(object sender, RoutedEventArgs e)
        {


        }

        private void image1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (Dedrievragen[0].vraagTXT == textBlockVraag.Text)
            {
                Correct = true;
                Score++;
                textBlockVraag.Text = "Correct !!!";
                GetTheQuestions(Score);
                Reedsgesteldevragen.Add(Dedrievragen[0]);
            }
            else WrongAnswer();
        }

        private void image2_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (Dedrievragen[1].vraagTXT == textBlockVraag.Text)
            {
                Correct = true;
                Score++;
                textBlockVraag.Text = "Correct !!!";
                GetTheQuestions(Score);
                Reedsgesteldevragen.Add(Dedrievragen[1]);
            }
            else WrongAnswer();
        }

        private void image3_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (Dedrievragen[2].vraagTXT == textBlockVraag.Text)
            {
                Correct = true;
                Score++;
                textBlockVraag.Text = "Correct !!!";
                GetTheQuestions(Score);
                Reedsgesteldevragen.Add(Dedrievragen[2]);
            }
            else WrongAnswer();
        }

        private void UserControl_Loaded_1(object sender, RoutedEventArgs e)
        {

        }



    }
}
